/*   1:    */ package com.sie.driver.ftusbdriver.protocol;
/*   2:    */ 
/*   3:    */ import com.sie.driver.ftusbdriver.interfaces.SpiSlave;

/*   4:    */ import junit.framework.Assert;
/*   5:    */ 
/*   6:    */ public class FT_Spi_Slave
/*   7:    */   extends SpiSlaveThread
/*   8:    */ {
/*   9:    */   private static final int FT4222_SPI_SLAVE_SYNC_WORD = 90;
/*  10:    */   private static final int SPI_MASTER_TRANSFER = 128;
/*  11:    */   private static final int SPI_SLAVE_TRANSFER = 129;
/*  12:    */   private static final int SPI_SHORT_MASTER_TRANSFER = 130;
/*  13:    */   private static final int SPI_SHART_SLAVE_TRANSFER = 131;
/*  14:    */   private static final int SPI_ACK = 132;
/*  15:    */   private static final int SPI_QUERY_VER = 136;
/*  16:    */   private DECODE_STATE mDecodeState;
/*  17:    */   private int mSync;
/*  18:    */   private int mCmd;
/*  19:    */   private int mSn;
/*  20:    */   private int mBufferSize;
/*  21:    */   private int mCurrentBufferSize;
/*  22:    */   private byte[] mBuffer;
/*  23:    */   private int mCheckSum;
/*  24:    */   private int mWrSn;
/*  25:    */   private SpiSlave mSpiSlave;
/*  26:    */   private SpiSlaveListener mSpiSlaveListener;
/*  27:    */   private boolean mIsOpened;
/*  28:    */   
/*  29:    */   private static enum DECODE_STATE
/*  30:    */   {
/*  31:113 */     STATE_SYNC,  STATE_CMD,  STATE_SN,  STATE_SIZE_HIGH,  STATE_SIZE_LOW,  STATE_COLLECT_DATA,  STATE_CHECKSUM_HIGH,  STATE_CHECKSUM_LOW;
/*  32:    */   }
/*  33:    */   
/*  34:    */   public FT_Spi_Slave(SpiSlave pSlaveInterface)
/*  35:    */   {
/*  36:150 */     this.mSpiSlave = pSlaveInterface;
/*  37:151 */     this.mDecodeState = DECODE_STATE.STATE_SYNC;
/*  38:    */   }
/*  39:    */   
/*  40:    */   public void registerSpiSlaveListener(SpiSlaveListener pListener)
/*  41:    */   {
/*  42:163 */     this.mSpiSlaveListener = pListener;
/*  43:    */   }
/*  44:    */   
/*  45:    */   public int open()
/*  46:    */   {
/*  47:174 */     if (this.mIsOpened) {
/*  48:175 */       return 1;
/*  49:    */     }
/*  50:176 */     this.mIsOpened = true;
/*  51:177 */     this.mSpiSlave.init();
/*  52:178 */     start();
/*  53:179 */     return 0;
/*  54:    */   }
/*  55:    */   
/*  56:    */   public int close()
/*  57:    */   {
/*  58:189 */     if (!this.mIsOpened) {
/*  59:190 */       return 3;
/*  60:    */     }
/*  61:192 */     SpiSlaveRequestEvent event = new SpiSlaveRequestEvent(-1, 
/*  62:193 */       true, null, null, null);
/*  63:194 */     sendMessage(event);
/*  64:    */     
/*  65:196 */     this.mIsOpened = false;
/*  66:197 */     return 0;
/*  67:    */   }
/*  68:    */   
/*  69:    */   public int write(byte[] wrBuf)
/*  70:    */   {
/*  71:211 */     if (!this.mIsOpened) {
/*  72:212 */       return 3;
/*  73:    */     }
/*  74:214 */     if (wrBuf.length > 65536) {
/*  75:215 */       return 1010;
/*  76:    */     }
/*  77:216 */     int[] sizeTransferred = new int[1];
/*  78:217 */     int idx = 0;
/*  79:218 */     int wrSize = wrBuf.length;
/*  80:219 */     int checksum = getCheckSum(wrBuf, 90, 129, this.mWrSn, wrSize);
/*  81:    */     
/*  82:221 */     byte[] buffer = new byte[8 + wrBuf.length];
/*  83:222 */     buffer[(idx++)] = 0;
/*  84:223 */     buffer[(idx++)] = 90;
/*  85:224 */     buffer[(idx++)] = -127;
/*  86:225 */     buffer[(idx++)] = ((byte)this.mWrSn);
/*  87:226 */     buffer[(idx++)] = ((byte)((wrSize & 0xFF00) >> 8));
/*  88:227 */     buffer[(idx++)] = ((byte)(wrSize & 0xFF));
/*  89:229 */     for (int i = 0; i < wrBuf.length; i++) {
/*  90:231 */       buffer[(idx++)] = wrBuf[i];
/*  91:    */     }
/*  92:233 */     buffer[(idx++)] = ((byte)((checksum & 0xFF00) >> 8));
/*  93:234 */     buffer[(idx++)] = ((byte)(checksum & 0xFF));
/*  94:235 */     this.mSpiSlave.write(buffer, buffer.length, sizeTransferred);
/*  95:236 */     if (sizeTransferred[0] != buffer.length) {
/*  96:239 */       return 4;
/*  97:    */     }
/*  98:241 */     this.mWrSn += 1;
/*  99:242 */     if (this.mWrSn >= 256) {
/* 100:243 */       this.mWrSn = 0;
/* 101:    */     }
/* 102:244 */     return 0;
/* 103:    */   }
/* 104:    */   
/* 105:    */   private boolean check_valid_spi_cmd(int cmd)
/* 106:    */   {
/* 107:249 */     if ((cmd == 128) || (cmd == 130) || (cmd == 136)) {
/* 108:250 */       return true;
/* 109:    */     }
/* 110:251 */     return false;
/* 111:    */   }
/* 112:    */   
/* 113:    */   private int getCheckSum(byte[] sendBuf, int sync, int cmd, int sn, int bufsize)
/* 114:    */   {
/* 115:256 */     int sum = 0;
/* 116:257 */     if (sendBuf != null) {
/* 117:259 */       for (int idx = 0; idx < sendBuf.length; idx++) {
/* 118:261 */         sum += (sendBuf[idx] & 0xFF);
/* 119:    */       }
/* 120:    */     }
/* 121:265 */     sum += sync;
/* 122:266 */     sum += cmd;
/* 123:267 */     sum += sn;
/* 124:268 */     sum += ((bufsize & 0xFF00) >> 8);
/* 125:269 */     sum += (bufsize & 0xFF);
/* 126:270 */     return sum;
/* 127:    */   }
/* 128:    */   
/* 129:    */   private void spi_push_req_ack_queue()
/* 130:    */   {
/* 131:276 */     int idx = 0;
/* 132:277 */     byte[] buffer = new byte[8];
/* 133:    */     
/* 134:279 */     buffer[(idx++)] = 0;
/* 135:280 */     buffer[(idx++)] = 90;
/* 136:281 */     buffer[(idx++)] = -124;
/* 137:282 */     buffer[(idx++)] = ((byte)this.mSn);
/* 138:283 */     buffer[(idx++)] = 0;
/* 139:284 */     buffer[(idx++)] = 0;
/* 140:    */     
/* 141:    */ 
/* 142:287 */     int checksum = getCheckSum(null, 90, 132, this.mSn, 0);
/* 143:288 */     buffer[(idx++)] = ((byte)((checksum & 0xFF00) >> 8));
/* 144:289 */     buffer[(idx++)] = ((byte)(checksum & 0xFF));
/* 145:    */     
/* 146:291 */     int[] sizeTransferred = new int[1];
/* 147:292 */     this.mSpiSlave.write(buffer, buffer.length, sizeTransferred);
/* 148:    */   }
/* 149:    */   
/* 150:    */   private void sp_slave_parse_and_push_queue(byte[] rdBuf)
/* 151:    */   {
/* 152:298 */     boolean reset = false;
/* 153:299 */     boolean dataCorrupted = false;
/* 154:300 */     for (int i = 0; i < rdBuf.length; i++)
/* 155:    */     {
/* 156:302 */       int val = rdBuf[i] & 0xFF;
/* 157:304 */       switch (this.mDecodeState)
/* 158:    */       {
/* 159:    */       case STATE_CHECKSUM_HIGH: 
/* 160:308 */         if (val != 90)
/* 161:    */         {
/* 162:309 */           reset = true;
/* 163:    */         }
/* 164:    */         else
/* 165:    */         {
/* 166:312 */           this.mDecodeState = DECODE_STATE.STATE_CMD;
/* 167:313 */           this.mSync = val;
/* 168:    */         }
/* 169:316 */         break;
/* 170:    */       case STATE_CHECKSUM_LOW: 
/* 171:319 */         if (!check_valid_spi_cmd(val))
/* 172:    */         {
/* 173:321 */           reset = true;
/* 174:322 */           dataCorrupted = true;
/* 175:    */         }
/* 176:    */         else
/* 177:    */         {
/* 178:325 */           this.mCmd = val;
/* 179:    */         }
/* 180:326 */         this.mDecodeState = DECODE_STATE.STATE_SN;
/* 181:    */         
/* 182:328 */         break;
/* 183:    */       case STATE_CMD: 
/* 184:331 */         this.mSn = val;
/* 185:332 */         this.mDecodeState = DECODE_STATE.STATE_SIZE_HIGH;
/* 186:    */         
/* 187:334 */         break;
/* 188:    */       case STATE_COLLECT_DATA: 
/* 189:337 */         this.mBufferSize = (val * 256);
/* 190:338 */         this.mDecodeState = DECODE_STATE.STATE_SIZE_LOW;
/* 191:    */         
/* 192:340 */         break;
/* 193:    */       case STATE_SIZE_HIGH: 
/* 194:343 */         this.mBufferSize += val;
/* 195:344 */         this.mCurrentBufferSize = 0;
/* 196:345 */         this.mBuffer = new byte[this.mBufferSize];
/* 197:346 */         this.mDecodeState = DECODE_STATE.STATE_COLLECT_DATA;
/* 198:    */         
/* 199:    */ 
/* 200:349 */         break;
/* 201:    */       case STATE_SIZE_LOW: 
/* 202:352 */         this.mBuffer[this.mCurrentBufferSize] = rdBuf[i];
/* 203:353 */         this.mCurrentBufferSize += 1;
/* 204:354 */         if (this.mCurrentBufferSize == this.mBufferSize) {
/* 205:355 */           this.mDecodeState = DECODE_STATE.STATE_CHECKSUM_HIGH;
/* 206:    */         }
/* 207:357 */         break;
/* 208:    */       case STATE_SN: 
/* 209:360 */         this.mCheckSum = (val * 256);
/* 210:361 */         this.mDecodeState = DECODE_STATE.STATE_CHECKSUM_LOW;
/* 211:    */         
/* 212:363 */         break;
/* 213:    */       case STATE_SYNC: 
/* 214:366 */         this.mCheckSum += val;
/* 215:367 */         int dataCheckSum = getCheckSum(this.mBuffer, this.mSync, this.mCmd, this.mSn, this.mBufferSize);
/* 216:368 */         if (this.mCheckSum == dataCheckSum)
/* 217:    */         {
/* 218:370 */           if (this.mCmd == 128)
/* 219:    */           {
/* 220:372 */             spi_push_req_ack_queue();
/* 221:373 */             if (this.mSpiSlaveListener != null)
/* 222:    */             {
/* 223:375 */               SpiSlaveResponseEvent pEvent = new SpiSlaveResponseEvent(3, 0, 
/* 224:376 */                 this.mBuffer, null, null);
/* 225:377 */               this.mSpiSlaveListener.OnDataReceived(pEvent);
/* 226:    */             }
/* 227:    */           }
/* 228:    */         }
/* 229:    */         else {
/* 230:383 */           dataCorrupted = true;
/* 231:    */         }
/* 232:385 */         reset = true;
/* 233:    */       }
/* 234:391 */       if (dataCorrupted) {
/* 235:393 */         if (this.mSpiSlaveListener != null)
/* 236:    */         {
/* 237:395 */           SpiSlaveResponseEvent pEvent = new SpiSlaveResponseEvent(3, 1, 
/* 238:396 */             null, null, null);
/* 239:397 */           this.mSpiSlaveListener.OnDataReceived(pEvent);
/* 240:    */         }
/* 241:    */       }
/* 242:400 */       if (reset)
/* 243:    */       {
/* 244:402 */         this.mDecodeState = DECODE_STATE.STATE_SYNC;
/* 245:403 */         this.mSync = 0;
/* 246:404 */         this.mCmd = 0;
/* 247:405 */         this.mSn = 0;
/* 248:406 */         this.mBufferSize = 0;
/* 249:407 */         this.mCurrentBufferSize = 0;
/* 250:408 */         this.mCheckSum = 0;
/* 251:409 */         this.mBuffer = null;
/* 252:410 */         reset = false;
/* 253:411 */         dataCorrupted = false;
/* 254:    */       }
/* 255:    */     }
/* 256:    */   }
/* 257:    */   
/* 258:    */   protected boolean pollData()
/* 259:    */   {
/* 260:417 */     int status = 0;
/* 261:418 */     int[] rxSize = new int[1];
/* 262:419 */     status = this.mSpiSlave.getRxStatus(rxSize);
/* 263:420 */     if ((rxSize[0] > 0) && (status == 0))
/* 264:    */     {
/* 265:422 */       byte[] rdBuf = new byte[rxSize[0]];
/* 266:423 */       status = this.mSpiSlave.read(rdBuf, rdBuf.length, rxSize);
/* 267:424 */       if (status == 0) {
/* 268:426 */         sp_slave_parse_and_push_queue(rdBuf);
/* 269:    */       }
/* 270:    */     }
/* 271:429 */     if (status == 4) {
/* 272:431 */       if (this.mSpiSlaveListener != null)
/* 273:    */       {
/* 274:433 */         SpiSlaveResponseEvent pEvent = new SpiSlaveResponseEvent(3, 2, 
/* 275:434 */           this.mBuffer, null, null);
/* 276:435 */         this.mSpiSlaveListener.OnDataReceived(pEvent);
/* 277:    */       }
/* 278:    */     }
/* 279:    */     try
/* 280:    */     {
/* 281:439 */       Thread.sleep(10L);
/* 282:    */     }
/* 283:    */     catch (InterruptedException localInterruptedException) {}
/* 284:441 */     return true;
/* 285:    */   }
/* 286:    */   
/* 287:    */   protected void requestEvent(SpiSlaveEvent pEvent)
/* 288:    */   {
/* 289:446 */     if ((pEvent instanceof SpiSlaveRequestEvent))
/* 290:    */     {
/* 291:448 */       SpiSlaveResponseEvent responseEvent = null;
/* 292:449 */       switch (pEvent.getEventType())
/* 293:    */       {
/* 294:    */       case -1: 
/* 295:    */         break;
/* 296:    */       }
/* 297:    */     }
/* 298:    */     else
/* 299:    */     {
/* 300:470 */       Assert.assertTrue("processEvent wrong type" + pEvent.getEventType(), false);
/* 301:    */     }
/* 302:    */   }
/* 303:    */   
/* 304:    */   protected boolean isTerminateEvent(SpiSlaveEvent pEvent)
/* 305:    */   {
/* 306:477 */     if (!Thread.interrupted()) {
/* 307:478 */       return true;
/* 308:    */     }
/* 309:480 */     if ((pEvent instanceof SpiSlaveRequestEvent)) {
/* 310:482 */       switch (pEvent.getEventType())
/* 311:    */       {
/* 312:    */       case -1: 
/* 313:485 */         return true;
/* 314:    */       }
/* 315:    */     } else {
/* 316:490 */       Assert.assertTrue("processEvent wrong type" + pEvent.getEventType(), false);
/* 317:    */     }
/* 318:492 */     return false;
/* 319:    */   }
/* 320:    */ }



/* Location:           E:\code\demo\A108DevForBankComm\libs\d2xx.jar

 * Qualified Name:     com.ftdi.j2xx.protocol.FT_Spi_Slave

 * JD-Core Version:    0.7.0.1

 */